﻿using AdminStyem.Application.Command.CultivationCommand;
using AdminStyem.Application.DTO;
using AdminStyem.Domain.Entity;
using AdminStyem.Repositories;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdminStyem.Application.CommandHandler.CultivatioHandler
{
    public class CultivationShowCommandHandler : IRequestHandler<CultivationShowCommand, APIResult<List<CultivationDTO>>>
    {
        IBaseRespository<Cultivation> BaseCultivation;

        public CultivationShowCommandHandler(IBaseRespository<Cultivation> baseCultivation)
        {
            BaseCultivation = baseCultivation;
        }

        public Task<APIResult<List<CultivationDTO>>> Handle(CultivationShowCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<CultivationDTO>> ret = new APIResult<List<CultivationDTO>>();
            var cultivations = BaseCultivation.Queryable().Where(c => !c.IsDeleted).ToList();
            var cultivationDTOs = cultivations.Select(c => new CultivationDTO
            {
                CultivationId = c.CultivationId,
                Variety = c.Variety,
                Source = c.Source,
                CultivationDay = c.CultivationDay,
                CultivationBrand = c.CultivationBrand,
                CultivationHead = c.CultivationHead,
                CultivationTime = c.CultivationTime,
                CultivationPhoto = c.CultivationPhoto
            }).ToList();

            ret.Code = ResultCode.查询成功;
            ret.Message = ResultCode.查询成功.ToString();
            ret.Data = cultivationDTOs;

            return Task.FromResult(ret);
        }
    }
}
